<!--
 * @Author: your name
 * @Date: 2020-12-07 14:15:40
 * @LastEditTime: 2021-03-22 10:00:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\warranty\warrantyManage.vue
-->
<template>
    <div class="warrantyManage-mian-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.SN')}}</span>
                <el-input v-model="inverterSn" v-big style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.siteName')}}</span>
                <el-input v-model="siteName" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('warrantyManage.userName')}}</span>
                <el-input v-model="loginName" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.deviceTest.wifiSN')}}</span>
                <el-input v-big v-model="wifiSN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('m_497927264244072448')}}</span>
                <el-select v-model="enableFlag" filterable :placeholder="$t('pleaseSelect')" clearable >
                    <el-option
                        v-for="item in dictionary.ON_LINE_STATE"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('country')}}</span>
                <el-select v-model="nation" filterable :placeholder="$t('pleaseSelect')" clearable >
                    <el-option
                        v-for="item in nationList"
                        :key="item.enName"
                        :label="item.enName"
                        :value="item.enName">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.inverterType')}}</span>
                <el-select v-model="model" filterable :placeholder="$t('pleaseSelect')" clearable >
                    <el-option
                        v-for="item in inverterType"
                        :key="item.sid"
                        :label="item.inverterTypeName"
                        :value="item.inverterTypeName">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-button   type="primary" slot="reference"  icon='el-icon-download' class="marginR10" @click="()=>{$store.commit('setNewVisiblePvModules',true)}">{{$t('add')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='false' @selection-change="handleSelectionChange($event)">
            <template #installTime='{data}'>
                {{data.installTime?data.installTime.split(' ')[0]:''}}
            </template>
            <template #type='{data}'>
                {{returnTypeText(data)}}
            </template>
            <template #expireTime='{data}'>
                {{data.expireTime?data.expireTime.split(' ')[0]:''}}
            </template>
            <template #option='{data}'>
                <div class="option-box">
                    <el-button size='small' type="primary" icon="el-icon-edit"  @click='showEditModal(data)' class="marginR10" :title="$t('edit')"></el-button>
                    <el-button size='small' type="primary" icon="el-icon-download"  @click='downloadModal(data)' class="marginR10" :title="$t('devUpgrade.download')" v-if='returnFlag(data)'></el-button>
                    <el-button size='small' type="primary" icon="el-icon-s-management"  @click='pushRenew(data)' class="marginR10" :title="$t('renew.buy')" v-if='returnFlag(data)&&data.isSEACKIT!==0'></el-button>
                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('index.delete_item_confirm')"
                        @confirm='deleteRow(data.sid)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference" icon='el-icon-delete' :title="$t('delete')"></el-button>
                    </el-popconfirm>
                    <el-button v-if='data.batterySN' size='small' type="primary" icon="iconfont icon-dianchi"  @click="()=>{$store.commit('setApiVisible',true);editRow = data}" class="marginR10" :title="$t('batteryAnalysis.title')" ></el-button>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <add :nationList='nationList' :inverterType='inverterType' @uploadData='qualityGetPage' v-if='newVisiblePvModules'></add>
        <edit :nationList='nationList' :inverterType='inverterType' @uploadData='qualityGetPage' :editRow='editRow' :disabledFlag='disabledFlag' :disabledFlag2='disabledFlag2'></edit>
        <detailModal :editRow='editRow'></detailModal>
    </div>
</template>

<script>
import baseTable from '../../components/common/baseTable'
import add from './add'
import edit from './edit'
import detailModal from './batteryDetail.vue'
export default {
    components: {
        baseTable,
        add,
        edit,
        detailModal
    },
    props: {

    },
    data() {
        return {
            inverterSn:'',
            siteName:'',
            loginName:'',
            wifiSN:'',
            enableFlag:'',
            nation:'',
            model:'',
            size:10,
            current:1,
            total:1,
            dictionary:[],
            nationList:[],
            inverterType:[],
            dataRows:[],
            column:[{
                title: this.$t('devWifi.deviceSN'),
                value: 'inverterSn',
                slot: 'inverterSn',
            },
            {
                title: this.$t('devWifi.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
                width:'130'
            },
            {
                title: this.$t('device.inverter.inverterType'),
                value: 'model',
                slot: 'model',
                min_width:'110'
            },
            {
                title: this.$t('totalPower.InverterPower.siteId'),
                value: 'siteName',
                slot: 'siteName',
            },{
                title: this.$t('warrantyManage.userName'),
                value: 'loginName',
                slot: 'loginName',
            },{
                title: this.$t('warrantyStatistics.nation'),
                value: 'nation',
                slot: 'nation',
            },{
                title: this.$t('salePage.tradeTime'),
                value: 'installTime',
                slot: 'installTime',
                width:'130'
            },{
                title: this.$t('warrantyManage.warranty_period'),
                value: 'expireTime',
                slot: 'expireTime',
                width:'130'
            }
            ,
            {
                title: this.$t('sn.snStatus'),
                value: 'type',
                slot: 'type',
                width:'130'
            },
            {
                title: this.$t('control'),
                value: 'option',
                slot: 'option',
            }],
            loading:false,
            editRow:{},
            disabledFlag:true,
            disabledFlag2:true,

        };
    },
    computed: {
        deviceType(){
            let type= ''
            this.inverterType.forEach(item =>{
                if(item.inverterTypeName == this.model){
                    type = item.inverterType
                }
            })
            return type
        },
        newVisiblePvModules(){
            return this.$store.state.sysStore.newVisiblePvModules
        },
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.qualityGetPage()
        this.selectCountry()
        this.orderpaypalconfigGetInverterTypeList()
    },
    methods: {
        returnFlag(data){
            // if(data.auditState == 0){
            //     if(data.type == 0 || data.type == 1){
            //         return false
            //     }else if(data.type == 2){
            //         return false
            //     }
            // }else if(data.auditState == 1){
            //     if(data.type == 2){
            //         return false
            //     }
            //     if(data.type == 0){
            //         return true
            //     }
            // }else{
            //     return false
            // }
            console.log(data.inverterSn,'  ',data.auditState,data.type,data.isSEACKIT,'    ----auditState type isSEACKIT----')
            if(data.auditState == 0 && data.type == 0){
                return false
            }
            if(data.auditState == 0 && data.type == 1){
                return true
            }
            if(data.auditState == 0 && data.type == 2){
                return false
            }
            if(data.auditState == 1 && data.type == 2){
                return true
            }
            if(data.auditState == 1 && data.type == 0){
                return true
            }

        },
        returnFlag2(data){
            if(data.auditState == 0){
                if(data.type == 1){
                    return true
                }else{
                    return false
                }
            }else if(data.auditState == 1){
                if(data.type == 2){
                    return true
                }else{
                    return false
                }
            }else{
                return false
            }
        },
        returnTypeText(data){
            if(data.auditState == 0){
                if(data.type == 0 || data.type == 1){
                    return this.$t('warranty.Approval.Pending')
                }else if(data.type == 2){
                    return this.$t('warranty.now.edit')
                }
            }else if(data.auditState == 1){
                if(data.type == 2){
                    return this.$t('warranty.now.edit')
                }
                if(data.type == 1){
                    return this.$t('warranty.Approval.Pending')
                }
                if(data.type == 0){
                    return this.$t('warranty.now.ok')
                }
            }else{
                return ' - '
            }
        },
        async deleteRow(id){
            let res = await this.API.qualityDelete({
                id
            })
            if(this.comm.showMsg(res.success)){
                this.query()
            }
        },
        pushRenew(data){
            sessionStorage.setItem("selRow",JSON.stringify(data));
            this.$router.push({path:"insurancePay"})
        },
        downloadModal(row){
            let editRow=row;
            let deviceTypeName=editRow.deviceType == 16?editRow.deviceTypeName:this.comm.returnCode(this.inverterType,editRow.deviceType,'inverterType','inverterTypeName')
            this.comm.windowOpen('/quality/downloadQualityInvoice?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
        },
        showEditModal(data){
            // data.auditState = 0
            // data.type = 0
            this.editRow = data
            this.disabledFlag = this.returnFlag(data)
            this.disabledFlag2 = this.returnFlag2(data)
            this.$store.commit('setVisibleEdit',true)
        },
        async qualityGetPage(){
            this.loading = true
            let res = await this.API.qualityGetPage({
                size: this.size,
                current: this.current,
                inverterSn: this.inverterSn,
                siteName: this.siteName,
                loginName: this.loginName,
                wifiSN: this.wifiSN,
                enableFlag: this.enableFlag,
                nation: this.nation,
                model:this.model,
                deviceType:this.deviceType
            })
            this.loading = false
            this.dataRows = res.rows || []
            this.total = res.total
        },
        async selectCountry(){
            let res = await this.API.selectCountry()
            this.nationList = res
        },
        async orderpaypalconfigGetInverterTypeList(){
            let res = await this.API.orderpaypalconfigGetInverterTypeList({
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.inverterType = res
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'ON_LINE_STATE,WARRANT_INVERTER_TYPES,IS_PUBLIC,SALER_TYPE,INTELBARS_INVERTER_TYPES',
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.dictionary = res || {}
        },
        query(){
            this.current = 1
            this.qualityGetPage()
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.qualityGetPage()
        },
        handleCurrentChange(val) { 
          this.current = val
          this.qualityGetPage()
        },
        handleSelectionChange(val){
          this.selectDataList=val
        },
    },
};
</script>

<style  lang="less" scoped>
.warrantyManage-mian-box{
    
}
</style>

